/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.common.c;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import com.qihoo.c.a.a.g;
import com.qihoo.gamecenter.sdk.common.i.s;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.SSLHandshakeException;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;

public class e implements d {
    private static String a = "HttpImageAgentImpl";
    private static d b;
    private Context c;

    public static synchronized d a(Context paramContext) {
        if (b == null)
            b = new e(paramContext);
        return b;
    }

    private e(Context paramContext) {
        this.c = paramContext;
    }

    public String a(String paramString, int paramInt)
  {
    com.qihoo.gamecenter.sdk.common.i.c.a(a, new Object[] { "doGetLoginInfo ", paramString });
    a locala = a.a(this.c);
    locala.a(com.qihoo.gamecenter.sdk.common.a.c.g());
    1 local1 = new a.b(paramInt)
    {
      public String a(HttpResponse paramHttpResponse, int paramInt)
      {
        com.qihoo.gamecenter.sdk.common.i.c.a(e.a(), new Object[] { "stCode=", Integer.valueOf(paramInt), "&mode=", Integer.valueOf(this.a) });
        String str1 = null;
        if (paramHttpResponse != null)
        {
          InputStream localInputStream = null;
          try
          {
            Object localObject1;
            Object localObject2;
            Matcher localMatcher1;
            Object localObject3;
            Object localObject4;
            if ((paramInt == 302) && (this.a == 2))
            {
              localObject1 = paramHttpResponse.getFirstHeader("Location").getValue();
              localObject2 = Pattern.compile("[?&]code=([0-9a-zA-Z]*)").matcher((CharSequence)localObject1);
              localMatcher1 = Pattern.compile("[?&]state=([0-9a-zA-Z_]*)").matcher((CharSequence)localObject1);
              localObject3 = "";
              localObject4 = "";
              while (((Matcher)localObject2).find())
                localObject3 = ((Matcher)localObject2).group(1);
              while (localMatcher1.find())
                localObject4 = localMatcher1.group(1);
              try
              {
                JSONObject localJSONObject = new JSONObject();
                localJSONObject.put("code", localObject3);
                localJSONObject.put("state", localObject4);
                str1 = localJSONObject.toString();
              }
              catch (Exception localException2)
              {
                com.qihoo.gamecenter.sdk.common.i.c.b(e.a(), new Object[] { localException2.toString() });
              }
            }
            else if ((paramInt == 302) && (this.a == 3))
            {
              localObject1 = new JSONObject();
              localObject2 = paramHttpResponse.getFirstHeader("Location").getValue();
              localMatcher1 = Pattern.compile("[?#]access_token=([0-9a-zA-Z]*)").matcher((CharSequence)localObject2);
              localObject3 = Pattern.compile("[?&]state=([0-9a-zA-Z_]*)").matcher((CharSequence)localObject2);
              localObject4 = Pattern.compile("[?&]expires_in=([0-9]*)").matcher((CharSequence)localObject2);
              Matcher localMatcher2 = Pattern.compile("[?&]refresh_token=([0-9a-zA-Z]*)").matcher((CharSequence)localObject2);
              String str2 = "";
              String str3 = "";
              String str4 = "";
              String str5 = "";
              while (localMatcher1.find())
                str2 = localMatcher1.group(1);
              while (((Matcher)localObject3).find())
                str3 = ((Matcher)localObject3).group(1);
              while (((Matcher)localObject4).find())
                str4 = ((Matcher)localObject4).group(1);
              while (localMatcher2.find())
                str5 = localMatcher2.group(1);
              ((JSONObject)localObject1).put("access_token", str2);
              ((JSONObject)localObject1).put("state", str3);
              ((JSONObject)localObject1).put("expires_in", str4);
              ((JSONObject)localObject1).put("scope", "");
              ((JSONObject)localObject1).put("refresh_token", str5);
              str1 = ((JSONObject)localObject1).toString();
            }
            else
            {
              localInputStream = b.a(paramHttpResponse.getEntity());
              str1 = b.b(localInputStream);
              if (paramInt == 200)
                com.qihoo.gamecenter.sdk.common.a.c.a(b.a(paramHttpResponse));
            }
          }
          catch (Exception localIOException2)
          {
            com.qihoo.gamecenter.sdk.common.i.c.b(e.a(), new Object[] { localException1.toString() });
          }
          finally
          {
            if (localInputStream != null)
              try
              {
                localInputStream.close();
              }
              catch (IOException localIOException3)
              {
                com.qihoo.gamecenter.sdk.common.i.c.b(e.a(), new Object[] { localIOException3.toString() });
              }
          }
        }
        return ((String)(String)(String)(String)str1);
      }
    };
    return locala.a(paramString, local1, null);
  }

    public String a(String paramString)
  {
    a locala = a.a(this.c);
    locala.a(com.qihoo.gamecenter.sdk.common.a.c.g());
    return locala.a(paramString, new a.b()
    {
      public String a(HttpResponse paramHttpResponse, int paramInt)
      {
        String str1 = null;
        if (paramHttpResponse != null)
        {
          InputStream localInputStream = null;
          try
          {
            if (paramInt == 200)
            {
              Header[] arrayOfHeader = paramHttpResponse.getHeaders("sc");
              Object localObject1 = null;
              for (Object localObject3 : arrayOfHeader)
              {
                if (localObject3 == null)
                  continue;
                localObject1 = localObject3;
                break;
              }
              if (localObject1 != null)
              {
                ??? = localObject1.getValue();
                ??? = URLDecoder.decode((String)???, "UTF-8");
                ??? = URLDecoder.decode((String)???, "UTF-8");
                com.qihoo.gamecenter.sdk.common.i.c.a(e.a(), new Object[] { "decodedSc=", ??? });
                localInputStream = b.a(paramHttpResponse.getEntity());
                String str2 = com.qihoo.gamecenter.sdk.common.i.a.b(b.a(localInputStream), 23);
                JSONObject localJSONObject = new JSONObject();
                localJSONObject.put("sc", ???);
                localJSONObject.put("bitmap", str2);
                str1 = localJSONObject.toString();
              }
            }
          }
          catch (Exception localIOException2)
          {
            com.qihoo.gamecenter.sdk.common.i.c.b(e.a(), new Object[] { localException.toString() });
          }
          finally
          {
            if (localInputStream != null)
              try
              {
                localInputStream.close();
              }
              catch (IOException localIOException3)
              {
                com.qihoo.gamecenter.sdk.common.i.c.b(e.a(), new Object[] { localIOException3.toString() });
              }
          }
        }
        return ((String)str1);
      }
    }
    , null);
  }

    public String b(String paramString) {
        return b(paramString, null);
    }

    public String b(String paramString1, String paramString2) {
        a locala = a.a(this.c);
        locala.a(com.qihoo.gamecenter.sdk.common.a.c.g());
        return locala.b(paramString1, paramString2);
    }

    public String a(ArrayList paramArrayList, String paramString) {
        return a(paramArrayList, paramString, null);
    }

    public String a(ArrayList paramArrayList, String paramString1, String paramString2) {
        a locala = a.a(this.c);
        locala.a(com.qihoo.gamecenter.sdk.common.a.c.g());
        return locala.a(paramString1, paramArrayList, paramString2);
    }

    @Deprecated
    public String b(ArrayList paramArrayList, String paramString1, String paramString2) {
        String str = a(paramArrayList, paramString1, paramString2);
        return a(str, true);
    }

    private String a(String paramString, boolean paramBoolean) {
        if (!(TextUtils.isEmpty(paramString))) {
            JSONObject localJSONObject = null;
            try {
                localJSONObject = new JSONObject(paramString);
            } catch (JSONException localJSONException) {
                com.qihoo.gamecenter.sdk.common.i.c.b(a, new Object[] { localJSONException.toString() });
            }
            if (localJSONObject != null) {
                int i = localJSONObject.optInt("error_code", 0);
                if ((paramBoolean) || (i == 5)) {
                    String str = localJSONObject.optString("error_msg");
                    a(this.c, i, str);
                }
                localJSONObject = localJSONObject.optJSONObject("content");
                if (localJSONObject != null)
                    return localJSONObject.toString();
            }
        }
        return null;
    }

    public String a(String paramString1, String paramString2) {
        return a(paramString1, paramString2, "head_shot");
    }

    public String a(String paramString1, String paramString2, String paramString3) {
        String str = null;
        InputStream localInputStream = null;
        try {
            f localf = f.a(null);
            if (localf != null) {
                HttpParams localHttpParams = localf.getParams();
                if (localHttpParams != null) {
                    HttpConnectionParams.setConnectionTimeout(localHttpParams, 15000);
                    HttpConnectionParams.setSoTimeout(localHttpParams, 20000);
                    HttpClientParams.setRedirecting(localHttpParams, true);
                    HttpPost localHttpPost = new HttpPost(paramString1);
                    File localFile = new File(paramString2);
                    g localg = new g();
                    com.qihoo.c.a.a.a.d locald = new com.qihoo.c.a.a.a.d(localFile, "image/jpeg");
                    localg.a(paramString3, locald);
                    localHttpPost.setHeader("Charset", "UTF-8");
                    localHttpPost.setHeader("Cookie", com.qihoo.gamecenter.sdk.common.a.c.h());
                    localHttpPost.setEntity(localg);
                    HttpResponse localHttpResponse = localf.execute(localHttpPost);
                    if (localHttpResponse != null) {
                        int i = localHttpResponse.getStatusLine().getStatusCode();
                        if (i == 200) {
                            localInputStream = localHttpResponse.getEntity().getContent();
                            str = a(localInputStream);
                        }
                    }
                }
            }
        } catch (SSLHandshakeException localIOException2) {
            com.qihoo.gamecenter.sdk.common.i.c.b(a, new Object[] { localSSLHandshakeException.toString() });
        } catch (IOException localIOException4) {
            com.qihoo.gamecenter.sdk.common.i.c.b(a, new Object[] { localIOException3.toString() });
        } catch (Exception localIOException5) {
            com.qihoo.gamecenter.sdk.common.i.c.b(a, new Object[] { localException.toString() });
        } finally {
            if (localInputStream != null)
                try {
                    localInputStream.close();
                } catch (IOException localIOException6) {
                    com.qihoo.gamecenter.sdk.common.i.c.b(a, new Object[] { localIOException6.toString() });
                }
        }
        com.qihoo.gamecenter.sdk.common.i.c.a(a, new Object[] { "doPostHeadshotHttpResp result --> ", str, " ",
                paramString1 });
        return str;
    }

    private String a(InputStream paramInputStream) {
        BufferedReader localBufferedReader = null;
        try {
            localBufferedReader = new BufferedReader(new InputStreamReader(paramInputStream));
            StringBuilder localStringBuilder = new StringBuilder();
            while ((str1 = localBufferedReader.readLine()) != null) {
                String str1;
                if (localStringBuilder.length() != 0)
                    localStringBuilder.append('\r');
                localStringBuilder.append(str1);
            }
            String str2 = localStringBuilder.toString();
            return str2;
        } catch (Exception localException2) {
            com.qihoo.gamecenter.sdk.common.i.c.b(a, new Object[] { localException1.toString() });
        } finally {
            if (localBufferedReader != null)
                try {
                    localBufferedReader.close();
                } catch (Exception localException4) {
                    com.qihoo.gamecenter.sdk.common.i.c.b(a, new Object[] { localException4.toString() });
                }
        }
        return null;
    }

    private void a(Context paramContext, int paramInt, String paramString) {
        if ((paramInt == 0) || (TextUtils.isEmpty(paramString)) || (paramContext == null)
                || (!(paramContext instanceof Activity)))
            return;
        Activity localActivity = (Activity) paramContext;
        localActivity.runOnUiThread(new Runnable(paramContext, paramInt, paramString) {
            public void run() {
                s.a(this.a, this.b, this.c, true);
            }
        });
    }
}